/*
 * Copyright 2012-2016 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package sample.web;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;
import sample.client.HelloWorldService;

@Controller
public class SampleController {

    private static final Logger logger = LoggerFactory.getLogger(SampleController.class);
    @Value("${service.haha}")
    private String serviceHaha;
    @Value("${service.version}")
    private String serviceVersion;

	@Autowired
	private HelloWorldService helloWorldService;

	@GetMapping("/")
	@ResponseBody
	public String helloWorld() {
        logger.info("{}",serviceHaha);
	    return this.helloWorldService.getHelloMessage();
	}

	@GetMapping("/haha")
	@ResponseBody
	public String haha() {
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> entity = restTemplate.getForEntity(serviceHaha +"/haha", String.class);
		logger.info("调用了服务:{}",serviceHaha +"/haha");
		return entity.getBody();
	}

	@GetMapping("/version")
	@ResponseBody
	public String version() {
		return "当前服务版本:"+serviceVersion;
	}

	@GetMapping("/err")
	@ResponseBody
	public String error() {
		logger.error("this is an error!");
		return "请观察错误日志是否映射到本地文件！";
	}
}
